const Router = require('@koa/router')
const users = require('../data/user')
const router =  new Router()

router.get('/user',async ctx=>{
    ctx.body = users
})

function getRandom(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
router.post('/user',async ctx=>{
    const body = ctx.request.body
    // 接收用户
    const user = {
        id:getRandom(1, 999),
        account:body.account,
        password:body.password,
        nickname:body.nickname,
    }
    users.push(user)
    ctx.body = {
        errorCode:0,
        msg:'创建用户成功',
        request:`'请求方式：'${ctx.method}'，请求路径：' ${ctx.url}`
    }
})

module.exports = router